package fr.mywiki.model.jdbcAdapter.utils;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;

import fr.mywiki.model.jdbcAdapter.values.AbstractValueObject;
import fr.mywiki.model.queries.DatabaseAdapter;
import fr.mywiki.model.queries.ResultList;


/**
 * Contains several methods to access and use specific data.
 * 
 * @author tlombard
 */
public class DatabaseRecordFacade {
	public static final String TABLE_PREFIX = "";
	
	/**
	 * Creates a new empty record in the database.
	 * 
	 * @param table
	 *            the table in which the record will be added.
	 * @param idFieldName
	 *            name of the field which is the identifier of this table.
	 * @param idValue
	 *            string value of the identifier of this record.
	 * 
	 * @throws SQLException if the execution of the query threw an exception.
	 */
	public synchronized static void createEmptyRecord(String table,
			String idFieldName, Object idValue) throws SQLException {
		String sql = "INSERT INTO `" + TABLE_PREFIX + table + "` (`" + idFieldName + "`) VALUES ('"
				+ idValue.toString() + "');";
		DatabaseAdapter.executeUpdateQuery(sql);
	}

	/**
	 * Creates a new record in the database from the given ValueObject Object.
	 * 
	 * @param value a ValueObject Object which has to be saved in the database.
	 * @throws SQLException if the execution of the query threw an exception.
	 */
	public synchronized static void createRecord(AbstractValueObject value) throws SQLException {
		String sql = "INSERT INTO `" + TABLE_PREFIX + value.getType() 
				+ "` (" + ExportValueObjectManager.getFieldNamesList(value)
				+ ") VALUES (" + ExportValueObjectManager.getFieldValuesList(value) + ");";
		DatabaseAdapter.executeUpdateQuery(sql);
		value.setInDatabase(true);
	}
	
	/**
	 * Updates the record concerning the given value in the database.
	 * @throws SQLException if the execution of the query threw an exception.
	 */
	public synchronized static void updateRecord(AbstractValueObject value) throws SQLException {
		String sql = "UPDATE " + TABLE_PREFIX + value.getType() 
				+ " SET " + ExportValueObjectManager.getFieldEqualitiesList(value)
				+ " WHERE `" + value.getIdentifierFieldName() + "` = '" + value.getIdentifier() + "'";
		DatabaseAdapter.executeUpdateQuery(sql);
	}
	
	/**
	 * Removes from the database the record corresponding to the given value object.
	 * 
	 * @param value an AbstractValueObject object
	 * @throws SQLException if the removal was not correctly done.
	 */
	public synchronized static void removeRecord(AbstractValueObject value) throws SQLException {
		String sql = "DELETE FROM `" + TABLE_PREFIX + value.getType() 
				+ "` WHERE `" + value.getIdentifierFieldName() + "` = '"+ value.getIdentifier() + "';";
		DatabaseAdapter.executeUpdateQuery(sql);
	}
	
	/**
	 * Connects to the database loads the record corresponding to the given
	 * identifier and table.
	 * 
	 * @param fromTable
	 *            the type of the desired record.
	 * @param identifierName
	 *            name of the column which is the identifier of the table.
	 * @param identifierValue
	 *            object which should enable finding the correct element of the
	 *            table.
	 * @return a <code>Object[]</code> array containing the different fields
	 *         of the concerned record.
	 * @throws SQLException
	 *             if the query did not work well.
	 */
	public static Object[] loadRecord(String type, String identifierName,
			Object identifierValue) throws SQLException {
		String sql = "SELECT * FROM " + TABLE_PREFIX + type 
				+ " WHERE " + identifierName + " = " + identifierValue.toString();
		return DatabaseAdapter.executeSelectQuery(sql).getRecord(0);
	}
	
	/**
	 * Connects to the database, loads all the records corresponding to the given identifiers and
	 * table.
	 * 
	 * @param fromTable the name of the table where the desired records should be.
	 * @param identifierName name of the column which is the identifier of the table.
	 * @param identifierValues a collection containing the identifiers od the wanted records.
	 * @return a resultSet containing the complete base records.
	 * @throws SQLException if the query did not work well.
	 */
	public static ResultList loadRecords(String fromTable, String identifierName,
			Collection identifierValues) throws SQLException {
		StringBuffer where = new StringBuffer();
		Iterator itValues = identifierValues.iterator();
		if (itValues.hasNext())
			where.append(" WHERE " + identifierName + " = " + (String) itValues.next() + " ");
		while (itValues.hasNext())
			where.append("OR " + identifierName + " = " + (String) itValues.next() + " ");
		String sql = "SELECT * FROM " + TABLE_PREFIX + fromTable + where.toString();
		return DatabaseAdapter.executeSelectQuery(sql);
	}
}
